/**
 * @file xgame_ary.c
 *
 */

 /*********************
 *      INCLUDES
 *********************/
#include "xgame_ary.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

 /**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

uint16_t *ary_merge_to_left(uint16_t *ary, uint16_t count)
 {
    uint16_t *left = ary;
    const uint16_t *end = ary + count;
    for(; ary < end; ary++){
        if(*ary != 0) *left++ = *ary;
    }
    return left;
 }

 /*ensure that array elements are non-zero values*/
 uint16_t *ary_combine_to_left(uint16_t *ary, uint16_t count)
 {
    uint16_t *left = ary;
    const uint16_t *end = ary + count;
    uint16_t data = *ary++;
    for(; ary < end; ary++){
      if(*ary == data){
          *left++ = data * 2;
          ary++;
          if(ary >= end) return left;
          data = *ary;
      }
      else{
          *left++ = data;
          data = *ary;
      }
    }
    *left++ = data;
    return left;
 }

uint16_t ary_get_max(const uint16_t *ary, uint16_t count)
{
    uint16_t max = 0;
    const uint16_t *end = ary + count;
    for(; ary < end; ary++){
      if(*ary > max) max = *ary;
    }
    return max;
}
uint16_t ary_get_min(const uint16_t *ary, uint16_t count)
{
    uint16_t min = 0xffff;
    const uint16_t *end = ary + count;
    for(; ary < end; ary++){
      if(*ary < min) min = *ary;
    }
    return min;
}

void ary_cpy_r2l(uint16_t *left, const uint16_t *right, uint16_t count)
{
    const uint16_t *end = right - count;
    for(; right > end; right--) *left++ = *right;
}
void ary_cpy_l2r(uint16_t *right, const uint16_t *left, uint16_t count)
{
    uint16_t *end = right - count;
    for(; right > end; right--) *right = *left++;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
